<?php
/**
 * @title 792. 匹配子序列的单词数 - 二分查找
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String $s
     * @param String[] $words
     * @return Integer
     */
    function numMatchingSubseq($s, $words) {
        /**
         * @since 2024-01-24 统计字符串s每个字母出现的位置
         */
        $hash = [];

        /**
         * @since 2024-01-24 遍历字符串s
         */
        $sLength = strlen($s);
        for($i=0; $i<$sLength; $i++){
            $hash[$s[$i]][] = $i;
        }
        // return $hash;

        /**
         * @since 2024-01-24 字符串s的子序列单词数量
         */
        $wordCount = 0;

        /**
         * @since 2024-01-24 遍历单词数组words
         */
        foreach ($words as $word){
            /**
             * @since 2024-01-24 单词word是否是字符串s的子序列
             */
            $p = -1;
            for($i=0; $i<strlen($word); $i++){
                /**
                 * @since 2024-01-24 字符串s中没有字母c
                 */
                $c = $word[$i];
                if(!isset($hash[$c])){
                    break;
                } else {}

                /**
                 * @since 2024-01-24 二分查找
                 */
                $s = 0;
                $e = count($hash[$c])-1;
                while($s <= $e){
                    $m = ($s+$e)>>1;
                    $v = $hash[$c][$m];

                    if($v < $p){
                        $s = $m+1;
                    } elseif($v >= $p){
                        $e = $m-1;
                    } else {}
                }

                /**
                 * @since 2024-01-24 字母c，在字符串s找到位置>p
                 */
                if(isset($hash[$c][$s])){
                    $p = $hash[$c][$s]+1;
                } else {
                    /**
                     * @since 2024-01-24 没有找到字符c的位置，break, i<strlen(word)
                     */
                    break;
                }
            }

            /**
             * @since 2024-01-24 单词word是字符串s的子序列
             */
            if($i == strlen($word)){
                $wordCount++;
            } else {}
            // echo "i={$i}, word={$word}, wordCount={$wordCount}\n";
        }

        /**
         * @return Integer
         */
        return $wordCount;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.24_2.php
 */
$datas = [
    ["abcde", ["a","bb","acd","ace"]], // 3
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->numMatchingSubseq(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
